using System;
using System.IO;
using MonoDevelop.Ide.Gui;

namespace MonoDevelop.ResourceEditor.Gui
{
	/// <summary>Defines the properties and methods for the ResourceEditorView IViewContent.</summary>
	public class ResourceEditorView : MonoDevelop.Ide.Gui.AbstractViewContent
	{
		#region Child Widgets
		ResourceEditorWidget resourceEditor = new ResourceEditorWidget();
		#endregion
		
		#region Properties
		/// <summary>Gets the Widget control to display.</summary>
		public override Gtk.Widget Control
		{
			get { return this.resourceEditor; }
		}
		
		/// <summary>Gets the ReadOnly state of the control.</summary>
		/// <value>True if the content is read only, otherwise false.</value>
		public override bool IsReadOnly
		{
			get { return false; }
		}
		
		/// <summary>Gets the Tab page label for the IViewContent.</summary>
		/// <value>A string containing the Tab page label.</value>
		public override string TabPageLabel 
		{
			get { return "Resource Editor"; }
		}
		#endregion
		
		/// <summary>Initializes a new instance of the ResourceEditorView IViewContent.</summary>
		public ResourceEditorView() : base()
		{
			this.resourceEditor.ContentChanged += new EventHandler(ResourceEditor_ContentChanged);
			this.resourceEditor.ShowAll();
		}
		
		/// <summary>Redraw the content for the IViewContent widget. </summary>
		public override void RedrawContent()
		{
		}
		
		/// <summary>Releases the resources used by the ResourceEditorView.</summary>
		public override void Dispose()
		{
			base.Dispose();
			this.resourceEditor.Dispose();
		}
		
		/// <summary>Load the specified file.</summary>
		/// <param name="fileName">A string containing the full path of the file to be loaded.</param>
		public override void Load(string fileName)
		{
			this.resourceEditor.LoadFile(fileName);
			this.ContentName = fileName;
			this.IsDirty = false;
		}
		
		/// <summary>Save the specified file.</summary>
		/// <param name="fileName">A string containing the full path of the file to be loaded.</param>
		public override void Save(string fileName)
		{
			this.OnBeforeSave(EventArgs.Empty);
			this.resourceEditor.SaveFile(fileName);
			this.ContentName = fileName;
			this.IsDirty = false;
		}
		
		/// <summary>Save the current content file.</summary>
		public override void Save()
		{
			this.Save(this.ContentName);
		}
		
		/// <summary>Set the IsDirty property when ever the content changes</summary>
		/// <param name="sender">An object that contains the sender data.</param>
		/// <param name="e">An EventArgs object that contains the event data.</param>
		private void ResourceEditor_ContentChanged(object sender, EventArgs e)
		{
			this.IsDirty = true;
		}
	}
	
}
